package view;

import domain.DomainController;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.ConnectException;

import java.util.ArrayList;
import java.util.List;
import javax.swing.BorderFactory;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.Border;

/**
 *
 * @author Joachim
 */
public class LoginFrame extends JFrame implements ActionListener {
    
    private JButton btnLogIn, btnQuit;
    private JTextField txtEmail;
    private JPasswordField txtPassword;
    
    private DomainController domainController;
    
    public LoginFrame(DomainController domainController) {
        this.domainController = domainController;
        initFrame();
        initComponents();
    }
    
    private void initFrame() {
        this.setSize(400,300);
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        this.setUndecorated(true);
        this.setVisible(true);
    }
    
    private void initComponents() {       
        JPanel BackgroundPanel = new BackgroundPanel(this, getClass().getResource("../resources/login.png"));
        
        JPanel loginPanel = new JPanel();
        loginPanel.setLayout(new BorderLayout());
        loginPanel.setOpaque(false);

        JPanel dataPanel = new JPanel();	
        dataPanel.setLayout(new GridBagLayout());
        dataPanel.setOpaque(false);
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.insets = new Insets(10,10,10,10);      
        {
            JLabel lblEmail = new JLabel("E-mail:");
            c.gridx = 0;
            c.gridy = 0;
            c.weightx = 1.0;
            dataPanel.add(lblEmail, c);
        }
        {
            txtEmail = new JTextField(15);
            c.gridx = 1;
            c.gridy = 0;
            c.weightx = 1.0;
            dataPanel.add(txtEmail, c);
        }
        {
            JLabel lblPassword = new JLabel("Password:");
            c.gridx = 0;
            c.gridy = 1;
            c.weightx = 1.0;
            dataPanel.add(lblPassword,c);
        }
        {
            txtPassword = new JPasswordField(15);
            c.gridx = 1;
            c.gridy = 1;
            c.weightx = 1.0;
            dataPanel.add(txtPassword, c);
        }
        loginPanel.add(dataPanel, BorderLayout.CENTER);

        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridBagLayout());
        buttonPanel.setOpaque(false);
        c.insets = new Insets(0,10,10,10);
        c.fill = GridBagConstraints.HORIZONTAL;
        {
            btnLogIn = new JButton("Log In");
            btnLogIn.addActionListener(this);
            c.gridx = 0;
            c.gridy = 0;	
            c.weightx = 0.0;
            buttonPanel.add(btnLogIn, c);
        }
        {
            btnQuit = new JButton("Quit");
            btnQuit.addActionListener(this);
            c.gridx = 1;
            c.gridy = 0;
            c.weightx = 0.0;
            buttonPanel.add(btnQuit, c);
        }
        loginPanel.add(buttonPanel, BorderLayout.SOUTH);

        add(loginPanel).setBounds(80,157,240,130);
        add(BackgroundPanel);
        getRootPane().setDefaultButton(btnLogIn);
        
        // Taskbar icons
        List<Image> icons = new ArrayList<Image>();
        icons.add(new ImageIcon(getClass().getResource("../resources/logos/logo32x32.png")).getImage());
        icons.add(new ImageIcon(getClass().getResource("../resources/logos/logo64x64.png")).getImage());
        icons.add(new ImageIcon(getClass().getResource("../resources/logos/logo128x128.png")).getImage());
        icons.add(new ImageIcon(getClass().getResource("../resources/logos/logo256x256.png")).getImage());
        icons.add(new ImageIcon(getClass().getResource("../resources/logos/logo512x512.png")).getImage());
        this.setIconImages(icons);
    }
    
    public void actionPerformed(ActionEvent evt) {
        if(evt.getSource() == btnLogIn) {
            try {
                boolean loginSuccess = domainController.logIn(txtEmail.getText(), txtPassword.getText());
                if(loginSuccess) {
                    JOptionPane.showMessageDialog(null, "Login successful");
                    new DropboxFrame(domainController).setVisible(true);
                    this.dispose();
                }
                else {
                    JOptionPane.showMessageDialog(null, "Login failed");
                }
            } catch(ConnectException ce) {
                JOptionPane.showMessageDialog(null, ce.getMessage());
            }
        }
        if(evt.getSource() == btnQuit) {
            System.exit(1);
        }
    }
    
}
